<script>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import request from '@/utils/request.js';
import { useRouter } from 'vue-router';

export default {
  name: 'UserRegister',
  setup() {
    const form = ref({
      username: '',
      password: '',
      password2: ''
    });

    const router = useRouter();

    // 提前定义 validateConfirmPassword 函数
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== form.value.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };

    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' },
        { validator: validateConfirmPassword, trigger: 'blur' }
      ]
    };

    const handleRegister = async () => {
        const response = await request.post('/user/register', form.value);
        ElMessage.success('注册成功');
        router.push('/user/login');
    };

    const handleReset = () => {
      form.value.username = '';
      form.value.password = '';
      form.value.password2 = '';
    };

    const handleBackToLogin = () => {
      router.push('/user/login');
    };

    return {
      form,
      rules,
      handleRegister,
      handleReset,
      handleBackToLogin
    };
  }
};
</script>

<template>
  <div class="register-container">
    <div class="background-image">
      <img src="@/assets/re_background.jpg" alt="Background" class="bg-img">
    </div>
    <div class="register-form">
      <el-card class="register-card">
        <h2>用户注册</h2>
        <el-form ref="registerForm" :model="form" :rules="rules" label-width="80px" @submit.prevent="handleRegister">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名" maxlength="16" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password" placeholder="请输入密码" maxlength="16" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="form.password2" placeholder="请再次输入密码" maxlength="16" show-word-limit></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit">注册</el-button>
            <el-button @click="handleReset">重置</el-button>
            <el-button @click="handleBackToLogin">返回登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  position: relative;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.bg-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.register-form {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
}

.register-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.9); /* 半透明背景 */
}
</style>
